package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Myinfoadater;
import com.woke.data.Datas_myinfo;
import com.woke.diyview.XListView;
import com.woke.diyview.XListView.IXListViewListener;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MyinfoActivity extends Activity implements OnItemClickListener, IXListViewListener {

	private XListView mListView;
	private MyApp application;
	private ArrayList<Datas_myinfo> datalist = new ArrayList<Datas_myinfo>();
	private LayoutInflater inflater;
	private int lksdj;
	private Myinfoadater adapter;
	private RelativeLayout mBar;
	private int pageno = 1 ;//�����ҳ��

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myinfo);
		
		inflater = getLayoutInflater();
		application = (MyApp) getApplication();
		intview();
		
	}

	private void intview() {
		
		mListView = (XListView) findViewById(R.id.avinfo_listview);
		mListView.setPullLoadEnable(true);
//		mListView.setOnScrollListener(this);
		mListView.setXListViewListener(this);
		mListView.setOnItemClickListener(this);
		adapter = new Myinfoadater(inflater,datalist);
		mListView.setAdapter(adapter);
		findViewById(R.id.fminfo_image_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mBar = (RelativeLayout) findViewById(R.id.avmyinfo_progress_bar);
		getmyinfodata(true);
	}
	
	@Override
	public void onRefresh() {
		pageno = 1;
		getmyinfodata(true);
	}

	@Override
	public void onLoadMore() {
		getmyinfodata(true);
	}

	private void getmyinfodata(final boolean galis) {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(MyinfoActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "message");
		params.put("a", "mymessage");
		params.put("p", pageno);
		params.put("user_id", application.getDatas_load().getId());
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("我的信息", ""+response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						String string = jsonObject.getString("data");
						JSONArray arra = jsonObject.getJSONArray("data");
						if(pageno == 1){
							datalist.clear();
						}
						ArrayList<Datas_myinfo> parseArray = (ArrayList<Datas_myinfo>) com.alibaba.fastjson.JSONArray.parseArray(string, Datas_myinfo.class);
						datalist.addAll(parseArray);
						adapter.notifyDataSetChanged();
						pageno = pageno+1;
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(MyinfoActivity.this, "失败！", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mListView.stopLoadMore();
				mListView.stopRefresh();
				mBar.setVisibility(View.GONE);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Datas_myinfo data_myinfo = datalist.get(lksdj);
		data_myinfo.setIs_read("2");
		adapter.notifyDataSetChanged();
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
		lksdj = arg2-1;
		Datas_myinfo data_myinfo = datalist.get(arg2-1);
		
		Intent intent = new Intent(MyinfoActivity.this,MyinfoinfoActivity.class);
		intent.putExtra("data_myinfo", data_myinfo);
		startActivityForResult(intent, lksdj);
	}
}
